package com.csvvip.cloud.controller;

import com.csvvip.cloud.feignClient.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "orderHystrix_fallback")
public class OrderHystrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/consumer/payment_ok/get/{id}")
    public String payment_ok(@PathVariable("id") Long id){
        return paymentHystrixService.payment_ok(id);
    }

    @GetMapping("/consumer/payment_timeout1/get/{id}")
    @HystrixCommand
    public String payment_timeout1(@PathVariable("id") Long id){
        int a = 10/0;
        return paymentHystrixService.payment_timeout(id);
    }

    @GetMapping("/consumer/payment_timeout2/get/{id}")
    public String payment_timeout2(@PathVariable("id") Long id){
        return paymentHystrixService.payment_timeout(id);
    }

    @GetMapping("/consumer/payment_timeout/get/{id}")
    @HystrixCommand( fallbackMethod = "payment_timeout_fallback" /*, commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds" , value = "1500")
    }*/)
    public String payment_timeout(@PathVariable("id") Long id){
        return paymentHystrixService.payment_timeout(id);
    }
    private String payment_timeout_fallback(@PathVariable("id") Long id){
        return "(自定义的处理方式。。。)我是消费者80端口，对方系统正忙，请稍后在试。";
    }

    private String orderHystrix_fallback(){
        return "(默认的处理方式。。。)我是消费者80端口，对方系统正忙，请稍后在试。";
    }
}
